package com.koron.tool.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.tool.bean.*;
import com.koron.tool.mapper.ToolAccountsManagerMapper;
import com.koron.tool.mapper.ToolRecordMapper;
import com.koron.tool.service.ToolUseRecordService;
import com.koron.tool.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: liaodf
 * @Date: 2021/10/21 17:01
 */
@Service
@Slf4j
public class ToolUseRecordServiceImpl implements ToolUseRecordService {

    @Override
    @TaskAnnotation("useRecordList")
    public Response useRecordList(SessionFactory factory, ToolUseRecordQuery query) {
        try {
            ToolRecordMapper mapper = factory.getMapper(ToolRecordMapper.class);
            ToolAccountsManagerMapper managerMapper = factory.getMapper(ToolAccountsManagerMapper.class);
            ToolGroupBean group = managerMapper.getGroup(query.getWarehouseId());

            List<ToolGroupBean> result = new ArrayList<>();
            List<ToolGroupBean> allChildList = ToolAccountsManagerServiceImpl.getAllChildList(managerMapper,
                    Arrays.asList(group), result);
            // 获取所有的分类编码
            List<String> codeList = allChildList.stream().map(bean -> bean.getCode()).collect(Collectors.toList());
            query.setToolTypeCodeList(codeList);
            PageHelper.startPage(query.getPage(), query.getPageSize());
            List<ToolUseRecordBean> list = mapper.useRecordList(query);
            PageInfo pageInfo = new PageInfo(list);
            return Response.succ(pageInfo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("查询失败");
        }
    }

    @Override
    @TaskAnnotation("addUseRecord")
    public Response addUseRecord(SessionFactory factory, EamUser user, ToolUseRecordBean bean) {
        try {
            ToolRecordMapper mapper = factory.getMapper(ToolRecordMapper.class);
            BeanUtil.add(user, bean);
            Integer value = mapper.insertUseRecord(bean);
            return Response.ok(value);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("保存失败");
        }
    }


    @Override
    @TaskAnnotation("inspectionRecordList")
    public Response inspectionRecordList(SessionFactory factory, ToolInspectionDetailQuery query) {
        ToolRecordMapper mapper = factory.getMapper(ToolRecordMapper.class);
        ToolAccountsManagerMapper managerMapper = factory.getMapper(ToolAccountsManagerMapper.class);
        ToolGroupBean group = managerMapper.getGroup(query.getWarehouseId());

        List<ToolGroupBean> result = new ArrayList<>();
        List<ToolGroupBean> allChildList = ToolAccountsManagerServiceImpl.getAllChildList(managerMapper,
                Arrays.asList(group), result);
        // 获取所有的分类编码
        List<String> codeList = allChildList.stream().map(bean -> bean.getCode()).collect(Collectors.toList());
        query.setToolTypeCodeList(codeList);
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<ToolInspectionDetailBean> list = mapper.inspectionRecordList(query);
        list.forEach(bean->{
            if (StringUtils.isEmpty(bean.getInspectionResult())){
                bean.setInspectionResult("合格");
            }
        });
        PageInfo pageInfo = new PageInfo(list);
        return Response.succ(pageInfo);
    }

    @Override
    @TaskAnnotation("scrapRecordList")
    public Response scrapRecordList(SessionFactory factory, ToolScrapQuery query) {
        ToolRecordMapper mapper = factory.getMapper(ToolRecordMapper.class);
        ToolAccountsManagerMapper managerMapper = factory.getMapper(ToolAccountsManagerMapper.class);
        ToolGroupBean group = managerMapper.getGroup(query.getWarehouseId());

        List<ToolGroupBean> result = new ArrayList<>();
        List<ToolGroupBean> allChildList = ToolAccountsManagerServiceImpl.getAllChildList(managerMapper,
                Arrays.asList(group), result);
        // 获取所有的分类编码
        List<String> codeList = allChildList.stream().map(bean -> bean.getCode()).collect(Collectors.toList());
        query.setToolTypeCodeList(codeList);
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<ToolScrapDetailBean> list = mapper.scrapRecordList(query);
        PageInfo pageInfo = new PageInfo(list);
        return Response.succ(pageInfo);
    }
}
